
/**************** 后台json数据 ********************/

var
  topMenu = [{
    "id": "7AC52EC3-E093-4174-8F78-01D854786D25",
    "text": "首页",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": ""
    },
    "children": null
  }, {
    "id": "F7E0E16E-7497-4D27-AB0F-1330DFE812E2",
    "text": "基础数据",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": ""
    },
    "children": null
  }, {
    "id": "DF73C0AE-18A2-4618-8950-3F04F6707CEC",
    "text": "合同签订",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": "/images/TreeImages/@book.gif"
    },
    "children": null
  }, {
    "id": "4A3CEFBD-4179-4579-873B-43F08535D1D8",
    "text": "合同执行",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": ""
    },
    "children": null
  }, {
    "id": "3B1E53ED-417E-4800-AE69-D841ACB26411",
    "text": "合同付款",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": ""
    },
    "children": null
  }, {
    "id": "74188CDA-BB0E-407A-A9AD-6A17B5AB9C70",
    "text": "动态成本",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": ""
    },
    "children": null
  }, {
    "id": "056EC7FE-3497-CAF2-6AB7-E69EB772965E",
    "text": "接口平台",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": "/newpm_zj/images/desktop/file_folder.png"
    },
    "children": null
  }, {
    "id": "056EC7FE-3497-CAF2-6AB7-E69EB772965E",
    "text": "接口平台2",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": "/newpm_zj/images/desktop/file_folder.png"
    },
    "children": null
  }, {
    "id": "056EC7FE-3497-CAF2-6AB7-E69EB772965E",
    "text": "接口平台3",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": "/newpm_zj/images/desktop/file_folder.png"
    },
    "children": null
  }, {
    "id": "056EC7FE-3497-CAF2-6AB7-E69EB772965E",
    "text": "接口平台4",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": "/newpm_zj/images/desktop/file_folder.png"
    },
    "children": null
  }, {
    "id": "056EC7FE-3497-CAF2-6AB7-E69EB772965E",
    "text": "接口平台5",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": "/newpm_zj/images/desktop/file_folder.png"
    },
    "children": null
  }, {
    "id": "056EC7FE-3497-CAF2-6AB7-E69EB772965E",
    "text": "接口平台6",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": "/newpm_zj/images/desktop/file_folder.png"
    },
    "children": null
  }, {
    "id": "056EC7FE-3497-CAF2-6AB7-E69EB772965E",
    "text": "接口平台7",
    "state": "closed",
    "pid": "9858903B-92C1-4979-BF5A-6402B461EBE7",
    "attributes": {
      "url": "",
      "img": "/newpm_zj/images/desktop/file_folder.png"
    },
    "children": null
  }],
  // 左侧菜单数据
  sildeMenu = [{
    "id": "E17EA2E5-92E8-4A0D-83BE-688FFE85CC64",
    "pid": "7AC52EC3-E093-4174-8F78-01D854786D25",
    "text": "审批事项",
    "state": "open",
    "attributes": {
      "url": "",
      "bgColor": "#f98985",
      "img": "&#xe618;"
    },
    "children": [{
      "id": "D0EC715B-7946-4997-8DDE-9207BE1B9D3E",
      "pid": "E17EA2E5-92E8-4A0D-83BE-688FFE85CC64",
      "text": "待办审批事项",
      "state": "open",
      "attributes": {
        "url": "inner00.html"
      },
      "children": []
    }, {
      "id": "6BFD7D99-5C31-4794-BFC9-376D503C6EA6",
      "pid": "E17EA2E5-92E8-4A0D-83BE-688FFE85CC64",
      "text": "已办审批事项",
      "state": "open",
      "attributes": {
        "url": "aspx/H3/YiBan_New.aspx",
        "img": "/newpm_zj/images/desktop/default.png"
      },
      "children": []
    }, {
      "id": "6BFD7D99-5C31-4794-BFC9-376D503C6EA62",
      "pid": "E17EA2E5-92E8-4A0D-83BE-688FFE85CC64",
      "text": "已办审批事项",
      "state": "open",
      "attributes": {
        "url": "aspx/H3/YiBan_New.aspx",
        "img": "/newpm_zj/images/desktop/default.png"
      },
      "children": []
    }]
  }, {
    "id": "51F2A4CA-582A-4E6F-8975-B2E34A626F4A",
    "pid": "7AC52EC3-E093-4174-8F78-01D854786D25",
    "text": "系统管理",
    "state": "open",
    "attributes": {
      "url": "",
      "bgColor": "#f98985",
      "img": "&#xe618;"
    },
    "children": [{
      "id": "D1B051FC-10D5-47DC-9877-1A4472688D1A",
      "pid": "51F2A4CA-582A-4E6F-8975-B2E34A626F4A",
      "text": "用户权限",
      "state": "open",
      "attributes": {
        "url": "",
        "img": "/newpm_zj/images/desktop/yhqx_b.png"
      },
      "children": [{
        "id": "9D2CE829-DA04-466E-9878-6BAE340F9AF21",
        "pid": "D1B051FC-10D5-47DC-9877-1A4472688D1A",
        "text": "菜单信息管理",
        "state": "open",
        "attributes": {
          "url": "aspx/SystemSet/SysMenu_List2.aspx",
          "img": "/newpm_zj/images/desktop/workflow.png"
        },
        "children": []
      },{
        "id": "9D2CE829-DA04-466E-9878-6BAE340F9AF22",
        "pid": "D1B051FC-10D5-47DC-9877-1A4472688D1A",
        "text": "菜单信息管理",
        "state": "open",
        "attributes": {
          "url": "aspx/SystemSet/SysMenu_List3.aspx",
          "img": "/newpm_zj/images/desktop/workflow.png"
        },
        "children": []
      },{
        "id": "9D2CE829-DA04-466E-9878-6BAE340F9AF23",
        "pid": "D1B051FC-10D5-47DC-9877-1A4472688D1A",
        "text": "菜单信息管理",
        "state": "open",
        "attributes": {
          "url": "aspx/SystemSet/SysMenu_List4.aspx",
          "img": "/newpm_zj/images/desktop/workflow.png"
        },
        "children": []
      }, {
        "id": "FF583EA4-B96E-4895-AD3A-18F0EB2073B0",
        "pid": "D1B051FC-10D5-47DC-9877-1A4472688D1A",
        "text": "角色信息管理",
        "state": "open",
        "attributes": {
          "url": "aspx/SystemSet/SystemRoles_Index.aspx",
          "img": "/newpm_zj/images/desktop/meeting.png"
        },
        "children": []
      }, {
        "id": "C1D8AF51-96EE-47B6-8BCF-5DD7DFF0C6F9",
        "pid": "D1B051FC-10D5-47DC-9877-1A4472688D1A",
        "text": "用户信息管理",
        "state": "open",
        "attributes": {
          "url": "aspx/SystemSet/SystemUserInfo.aspx",
          "img": "/newpm_zj/images/desktop/hr.png"
        },
        "children": []
      }, {
        "id": "26D0B44E-0393-4612-AAB7-C24A6DD7AFFF",
        "pid": "D1B051FC-10D5-47DC-9877-1A4472688D1A",
        "text": "按钮信息管理",
        "state": "open",
        "attributes": {
          "url": "aspx/SystemSet/SysButton_List.aspx",
          "img": "/newpm_zj/images/desktop/vote.png"
        },
        "children": []
      }]
    }, {
      "id": "desktop",
      "pid": "51F2A4CA-582A-4E6F-8975-B2E34A626F4A",
      "text": "个人桌面设置",
      "state": "open",
      "attributes": {
        "url": "aspx/SystemSet/MyDesktop_List.aspx",
        "img": "/newpm_zj/images/desktop/meeting.png"
      },
      "children": []
    }, {
      "id": "3EA69FD8-7E93-4832-AAF4-95A7F9AB736C",
      "pid": "51F2A4CA-582A-4E6F-8975-B2E34A626F4A",
      "text": "系统更新日志",
      "state": "open",
      "attributes": {
        "url": "aspx/SystemSet/SystemUpdateLog_Index.aspx",
        "img": "/newpm_zj/images/desktop/htqdgl_b.png"
      },
      "children": []
    }]
  }]

$(function() {
  var
    pmNav = $('.pm-header>.pm-nav'),
    pmBody = $('.pm-main>.pm-body'),
    pmiFrame = $('.pm-main .iframe-wrap'),
    pmSider = $('.pm-body>.pm-sider'),
    pmMenu = $('.pm-main .pm-menu'),
    topMenuHtml = '',
    topMenuUl = pmNav.find('.pm-nav-ul'),
    topMenuIndex = 0,
    moreBtn = pmNav.find('.nav-more>button'),
    moreList = pmNav.find('.more-list'),
    moreNav = pmNav.find('.nav-more'),
    crumbs = $('.pm-crumbs'),
    siderMunuHtml = '',
    siderOpen = pmSider.find('.oc-i'),
    crumbUL = crumbs.find('ul'),
    prjNameBtn = $('.pm-main .pm-header .prj-name'),
    prjNameDrop = $('.pm-main .pm-header .prj-name-drop'),
    userBtn = $('.pm-main .pm-header .user'),
    userDrop = $('.pm-main .pm-header .user-drop')

  $.messager.defaults.ok = '确认'
  $.messager.defaults.cancel = '取消'

  // 生成顶部菜单
  $.each(topMenu, function(index) {
    if (index == 0) {
      topMenuHtml +=
        '<li class="pm-nav-li act">' +
          '<button>'+ this.text +'</button>' +
        '</li>'
    } else {
      topMenuHtml +=
        '<li class="pm-nav-li">' +
          '<button>'+ this.text +'</button>' +
        '</li>'
    }
  })

  topMenuUl.append(topMenuHtml)

  // 生成左侧菜单
  function createMenu(menuData) {
    pmMenu.html('')
    function createItem(menu, parent) {
      $.each(menu, function() {
        var curhtml = ''
        var curNode = null
        if (parent[0] === pmMenu[0]) {
          curhtml =
            '<div class="menu-item">' +
              '<div class="item-head leve1-h clearfix" data-id="'+ this.id +'" data-url="'+ this.attributes.url +'">' +
                '<i class="pm-icon icon" style="background-color: '+ this.attributes.bgColor +';">'+ this.attributes.img +'</i>' +
                '<span>'+ this.text +'</span>' +
                '<i class="pm-icon arrow">&#xe672;</i>' +
              '</div>' +
              '<div class="item-body leve1-b"></div>' +
            '</div>'
        } else if (parent.hasClass('leve1-b')) {
          curhtml =
            '<div class="menu-item">' +
              '<div class="item-head leve2-h clearfix" data-id="'+ this.id +'" data-url="'+ this.attributes.url +'">' +
                '<i class="arrow right"></i>' +
                '<span>'+ this.text +'</span>' +
              '</div>' +
              '<div class="item-body leve2-b"></div>' +
            '</div>'
        } else {
          curhtml =
            '<div class="menu-item">' +
              '<div class="item-head clearfix" data-id="'+ this.id +'" data-url="'+ this.attributes.url +'">' +
                '<i class="arrow right"></i>' +
                '<span>'+ this.text +'</span>' +
              '</div>' +
              '<div class="item-body"></div>' +
            '</div>'
        }

        curNode = $(curhtml)

        if (this.children.length === 0) {
          curNode.find('.item-head .arrow').removeClass('arrow').removeClass('right').addClass('point')
          curNode.find('.item-body').remove()
        }
        parent.append(curNode)
        createItem(this.children, curNode.find('.item-body'))
      })
    }

    createItem(menuData, pmMenu)
  }

  createMenu(sildeMenu)

  // 调整面包屑宽度
  function computedMbx() {
    var
      crumblis = crumbs.find('ul li'),
      aw = 0,
      curW = 0

    curW = crumblis.length * 140
    aw = crumbs.width() - 120

    if (aw < curW) {
      crumblis.each(function() {
        $(this).css('width', aw  / crumblis.length)
      })
    } else {
      crumblis.each(function() {
        $(this).css('width', 140)
      })
    }
  }

  // 设置顶部菜单布局
  function setTopMenu() {
    var
      ww = window.innerWidth,
      topMenuLis = pmNav.find('>ul li'),
      lwCount = 0,
      overIndex = -1,
      moreli = ''

    if (ww > 574) pmNav.css('width', ww - 584)

    topMenuLis.each(function(index) {
      lwCount += this.clientWidth
      if (lwCount > pmNav.width() - 97) {
        overIndex = index
        return false
      }
    })

    if (overIndex > -1) {
      for (var i = overIndex; i < topMenuLis.length; i++) {
        if (i == topMenuIndex) {
          moreli += '<li class="act" data-index="'+ i +'"><button>'+ topMenuLis.eq(i).find('button').text() +'</button></li>'
        } else {
          moreli += '<li data-index="'+ i +'"><button>'+ topMenuLis.eq(i).find('button').text() +'</button></li>'
        }
      }

      moreList.find('ul').html(moreli)
      moreNav.show()
    } else {
      moreNav.hide()
    }
  }

  setTopMenu()

  // 布局
  function setPageLayer() {
    pmBody.css('height', window.innerHeight - 60)
    pmMenu.css('height', pmMenu.parent().height() - 20)
    pmiFrame.css('height', window.innerHeight - 108)
    computedMbx()
  }

  setPageLayer()

  $(window).on('resize', setPageLayer)
  $(window).on('resize', setTopMenu)
  $(window).on('resize', computedMbx)

  // 顶部更多按钮
  moreBtn.click(function(ev) {
    moreList.css('display', moreList.css('display') == 'block' ? 'none' : 'block')
  })

  function isChild(child, parent) {
    while(child) {
      if (child === parent) return true
      child = child.parentNode
    }
    return false
  }

  // 隐藏下拉菜单
  $(document).on('mousedown', function(ev) {
    if (moreList.css('display') !== 'none') {
      if (!isChild(ev.target, moreNav[0])) {
        moreList.css('display', 'none')
      }
    }
    if (prjNameDrop.css('display') !== 'none') {
      if (!isChild(ev.target, prjNameBtn[0])) {
        prjNameDrop.css('display', 'none')
      }
    }
    if (userDrop.css('display') !== 'none') {
      if (!isChild(ev.target, userBtn[0])) {
        userDrop.css('display', 'none')
      }
    }
  })

  function iframeActHideDrop() {
    var actDoc = document.activeElement
    if (actDoc.tagName === 'IFRAME') {
      moreList.css('display', 'none')
      prjNameDrop.css('display', 'none')
      userDrop.css('display', 'none')
    }
    setTimeout(iframeActHideDrop, 1000 / 60)
  }

  iframeActHideDrop()

  // 顶部菜单交互
  topMenuUl.delegate('.pm-nav-li button', 'click', function(ev) {
    var
      item = $(ev.target),
      content = $('.pm-main .pm-body .pm-content')

    curIndex = item.parent().index()
    pmNav.find('li').removeClass('act')
    item.parent().addClass('act')

    moreList.css('display', 'none')

    createMenu(sildeMenu)

    if (pmSider.hasClass('open')) {
      pmMenu.find('.leve1-h').eq(0).trigger('click')
    } else {
      pmSider.find('.menu-item').css({height: 60, overflow: 'hidden'})
    }
  })

  // 点击顶部下拉菜单
  moreList.delegate('button', 'click', function() {
    var
      li = $(this).parent(),
      meitem = pmSider.find('.menu-item'),
      content = $('.pm-main .pm-body .pm-content')

    curIndex = li.attr('data-index')

    pmNav.find('li').removeClass('act')
    li.addClass('act')

    moreList.css('display', 'none')

    createMenu(sildeMenu)

    pmSider.addClass('open')
    pmSider.css({width: 240})
    meitem.css({height: 'auto', overflow: 'visible'})
    content.css('padding-left', 250)
    siderOpen.removeClass('oc-c').addClass('oc-o')

    pmMenu.find('.leve1-h').eq(0).trigger('click')
  })

  // 收起展开左侧菜单
  siderOpen.click(function() {
    var
      icon = $(this),
      meitem = pmSider.find('.menu-item'),
      content = $('.pm-main .pm-body .pm-content')

    if (pmSider.hasClass('open')) {
      pmSider.removeClass('open')
      pmSider.css({width: 50})
      meitem.css({height: 60, overflow: 'hidden'})
      content.css('padding-left', 60)
      icon.removeClass('oc-o').addClass('oc-c')
    } else {
      pmSider.addClass('open')
      pmSider.css({width: 240})
      meitem.css({height: 'auto', overflow: 'visible'})
      content.css('padding-left', 250)
      icon.removeClass('oc-c').addClass('oc-o')
    }
  })

  // 左侧菜单交互
  pmMenu.delegate('.item-head', 'click', function() {
    var
      head = $(this),
      body = head.next(),
      bh = body.height(),
      crumbLIs = crumbUL.find('li'),
      hasCrumb = false,
      inner = $('.pm-popup').find('.popup-inner')

    if (head.hasClass('leve1-h') && !head.hasClass('act')) {
      $('.pm-menu .leve1-h').removeClass('sel').removeClass('act')
      $('.pm-menu .item-head').removeClass('act')
      $('.pm-menu .item-head .arrow').removeClass('down').addClass('right')
      $('.pm-menu .item-body').css('display', 'none')
    }

    if (body.length === 0) {
      var
        name = head.find('span').text(),
        id = head.attr('data-id'),
        url = head.attr('data-url')

      crumbLIs.each(function() {
        if ($(this).attr('data-id') === id) {
          crumbUL.find('li').removeClass('act')
          $(this).addClass('act')
          $('.iframe-wrap iframe').css('display', 'none')
          $('#iframe-' + id).css('display', 'block')
          hasCrumb = true
        }
      })

      if (!hasCrumb && crumbLIs.length >= 10) {
        // easyui 提示插件
        $.messager.alert('提示', '当前打开的页面数量过多，请关闭部分页面后再进行操作。')
        return
      }

      $('.pm-menu .item-head').removeClass('sel')
      head.addClass('sel')

      if (!hasCrumb && crumbLIs.length < 10) {
        crumbUL.find('li').removeClass('act')
        crumbUL.append(
          '<li class="act" data-id="'+ id +'">' +
            '<span title="'+ name +'">'+ name +'</span>' +
            '<i class="pm-icon">&#xe68f;</i>' +
          '</li>'
        )

        $('.iframe-wrap').find('iframe').css('display', 'none')
        $('.iframe-wrap').append('<iframe id="iframe-'+ id +'" frameborder="0" src="'+ url +'"></iframe>')

        computedMbx()
      }
      return
    }

    if (head.hasClass('act')) {
      body.css('display', 'none')
      head.removeClass('act')
      if (head.hasClass('leve1-h')) {
        head.find('.arrow').html('&#xe672;')
      } else {
        head.find('.arrow').removeClass('down').addClass('right')
      }
    } else {
      head.addClass('act')
      if (head.hasClass('leve1-h')) {
        head.find('.arrow').html('&#xe650;')
      } else {
        head.find('.arrow').removeClass('right').addClass('down')
      }
      body.height(0).css('display', 'block')
      body.animate({height: bh}, 200, function() {
        body.css('height', 'auto')
      })
    }
  })

  // 面包屑交互
  crumbUL.delegate('li', 'click', function() {
    var
      li = $(this),
      id = li.attr('data-id')

    li.parent().find('li').removeClass('act')
    li.addClass('act')

    $('.iframe-wrap').find('iframe').css('display', 'none')
    $('.iframe-wrap').find('#iframe-'+id).css('display', 'block')
  })

  crumbUL.delegate('i', 'click', function(ev) {
    var
      li = $(this).parent(),
      ul = li.parent(),
      prev = li.prev()

    ev.stopPropagation()

    if (ul.find('li').length < 2) return

    if (!li.hasClass('act')) {
      li.remove()
      computedMbx()
      return
    }

    if (prev.length == 0) prev = li.next()

    $('.iframe-wrap iframe').css('display', 'none')
    $('#iframe-' + prev.attr('data-id')).css('display', 'block')
    $('#iframe-' + li.attr('data-id')).remove()

    ul.find('li').removeClass('act')
    prev.addClass('act')
    li.remove()

    computedMbx()
  })

  $('.pm-main .pm-crumbs .close-all').click(function() {
    crumbUL.find('li').remove()
    crumbUL.append(
      '<li class="act" data-id="desktop">' +
        '<span title="个人桌面">个人桌面</span>' +
        '<i class="pm-icon">&#xe68f;</i>' +
      '</li>'
    )
    $('.iframe-wrap iframe').remove()
    $('.iframe-wrap').append('<iframe id="iframe-desktop" frameborder="0" src="desktop.html"></iframe>')
    computedMbx()
  })

  // 项目名下拉框
  prjNameBtn.click(function() {
    var curSta = prjNameDrop.css('display')
    prjNameDrop.css('display', curSta == 'none' ? 'block' : 'none')
  })

  prjNameDrop.delegate('li', 'click', function() {
    prjNameBtn.find('>span').text($(this).text())
  })

  // 用户名下拉框
  userBtn.click(function() {
    var curSta = userDrop.css('display')
    userDrop.css('display', curSta == 'none' ? 'block' : 'none')
  })

  userDrop.delegate('li', 'click', function() {
    userBtn.find('>span').text($(this).text())
  })

  $.messager.show({
    title: '错误提示',
    msg: '请选择您要查看的明细数据...',
    timeout: 200000,
    showType: 'slide'
  })
})
